package com.easemob.chatuidemo.activity;

import java.util.concurrent.CountDownLatch;

import u.aly.bi;
import u.aly.co;
import u.aly.da;

import com.app.DemoApplication;
import com.common.method.ServerPath;
import com.contact.UserBaseInfoActivity;
import com.google.android.imageloader.ImageLoader;
import com.http.UserInfoTask;
import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.bitmap.BitmapDisplayConfig;
import com.lidroid.xutils.bitmap.callback.BitmapLoadCallBack;
import com.lidroid.xutils.bitmap.callback.BitmapLoadFrom;
import com.matang.R;
import com.modle.User;
import com.util.UserUtil;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

/**
 * @author 用户信息
 *
 */
public class UserInfoFragment extends Fragment implements UserInfoTask.CallBack{
	private View view;
	private ImageView imgBack;
	private RelativeLayout relaSet;
	private ImageView imghead;
	private TextView tvName,tvMlh,tvEdit;
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		
		view = inflater.inflate(R.layout.user_info, container,false);
		init();
		loadUserData();
	
		return view;
	}
	private void init(){
		imghead = (ImageView) view.findViewById(R.id.img_head);
		tvName = (TextView) view.findViewById(R.id.tv_user_nama);
		tvMlh = (TextView) view.findViewById(R.id.tv_mlh);
		tvEdit = (TextView) view.findViewById(R.id.tv_edit);
		relaSet = (RelativeLayout) view.findViewById(R.id.rela_set);
		imgBack = (ImageView) view.findViewById(R.id.img_back);
		imgBack.setOnClickListener(new OnClickListener() {
			
		
			public void onClick(View v) {
				getActivity().finish();
				
			}
		});
		relaSet.setOnClickListener(new OnClickListener() {
			
		
			public void onClick(View v) {
				Intent intent = new Intent();
				intent.setClass(getActivity(), SettingsActivity.class);
				startActivity(intent);
				
			}
		});
		tvEdit.setOnClickListener(new OnClickListener() {
			
		
			public void onClick(View v) {
				User user =  UserUtil.readUser();
			
					Intent intent = new Intent();
					intent.putExtra("user",user);
					intent.setClass(getActivity(), UserBaseInfoActivity.class);
					startActivity(intent);
				
				
			}
		});
	}

	public Handler handler = new Handler(){
		public void handleMessage(android.os.Message msg) {
			Log.i("test", "刷新个人信息");
			loadUserData();
		};
	};
	private void loadUserData() {
		User user = UserUtil.readUser();
		if (user != null) {
			new UserInfoTask(this).execute(user.id);
		}
	
	}
	public void procError(String msg) {
		// TODO Auto-generated method stub
		
	}
//获取用户信息成功
	private int count = 0;
	public void procUserInfoData(User data) {
		String nikename = data.nickName;
		String id = data.id;
//		ImageLoader.get(getActivity()).bind(imghead,
//				ServerPath.SERVER+data.thumb, null,R.drawable.default_avatar);
		BitmapUtils bitmapUtils = new BitmapUtils(getActivity());
		bitmapUtils.display(imghead, ServerPath.SERVER+data.thumb, new BitmapLoadCallBack<View>() {

			@Override
			public void onLoadCompleted(View arg0, String arg1, Bitmap arg2,
					BitmapDisplayConfig arg3, BitmapLoadFrom arg4) {
				((ImageView)arg0).setImageBitmap(arg2);
				
			}

		
			public void onLoadFailed(View arg0, String arg1, Drawable arg2) {
				count ++;
				if (count < 3) {
					loadUserData();
				}else {
					((ImageView)arg0).setImageResource(R.drawable.default_avatar);
				}
			
				
			}
		});
	
		tvName.setText(nikename);
		tvMlh.setText("马聊号:"+id);
		
	}
	@Override
	public void procUserInfoError(String msg) {
	
		
	}

}
